﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using BLL;
using Model;
using System.Data.SqlClient;
using Common;
using System.Data;
using System.Web.UI.WebControls;
using System.Web;

namespace WebPortal.Controllers
{
    public class SysViewController : ViewInfo
    {   
        /// <summary>
        /// 用户组页面初始化
        /// </summary>
        /// <returns></returns>
        public ActionResult ViewGroupList(string dbName)
        {
            CheckTarCode(dbName);
            return View();

        }
        /// <summary>
        /// 取用户组信息页面初始化
        /// </summary>
        /// <param name="id">用户组id</param>
        /// <returns></returns>
        public ActionResult GetGroupItem(string dbName, string id)
        {
            CheckTarCode(dbName);
            if (id == null)//添加页面
                return View(new Model.SysViewGroup());
            else//修改页面
            {
                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                SysViewGroupBLL bll = new SysViewGroupBLL(loginInfos);
                SysViewGroup model = bll.GetItem(id);
                return View(model);
            }
        }
        /// <summary>
        /// 视图页面初始化
        /// </summary>
        /// <returns></returns>
        public ActionResult ViewList(string dbName)
        {
            CheckTarCode(dbName);
            string ida = DateTime.Now.ToString("yyyyMMddHHmmssfffffff");
            string guid = Guid.NewGuid().ToString().Replace("-", "");

            ida += guid.Substring(0, 10);
            return View();
        }
        /// <summary>
        /// 视图推送页面初始化
        /// </summary>
        /// <returns></returns>
        public ActionResult ViewPushList(string dbName)
        {
            CheckTarCode(dbName);
            return View();
        }
        /// <summary>
        /// 视图推送编辑页面初始化
        /// </summary>
        /// <param name="gid">用户组id</param>
        /// <returns></returns>
        public ActionResult GetPush(string gid, string dbName)
        {
            CheckTarCode(dbName);
            if (gid == null)//添加页面
                return View(new SysNameValue());
            else//编辑页面
            {
                Model.SysNameValue model = new SysNameValue();
                model.Name = "gid";
                model.Value = gid;
                return View(model);
            }
        }
        /// <summary>
        /// 视图审核页面初始化
        /// </summary>
        /// <returns></returns>
        public ActionResult ApproveList(string dbName)
        {
            CheckTarCode(dbName);
            return View();
        }
        /// <summary>
        /// 取视图审核信息
        /// </summary>
        /// <param name="id">视图id</param>
        /// <returns></returns>
        public ActionResult GetViewItem(string dbName, string id)
        {
            CheckTarCode(dbName);
            if (id == null)//添加页面
                return View(new Model.SysTBView());
            else//查看详细页面
            {

                LoginInfo loginInfos = base.loginInfo();
                loginInfos.DBName = dbName;
                SysTBViewBLL bll = new SysTBViewBLL(loginInfos);
                SysTBView model = bll.GetItem(id);
                return View(model);
            }
        }
        #region 旧方法
        /// <summary>
        /// 上传视图页面初始化
        /// </summary>
        /// <param name="id">视图id</param>
        /// <returns></returns>
        public ActionResult UpFile(string id)
        {
            CheckTarCode("");
            if (id == null)//添加页面
                return View(new Model.SysTBView());
            else//编辑页面
            {
                LoginInfo loginInfos = base.loginInfo();
                SysTBViewBLL bll = new SysTBViewBLL(loginInfos);
                SysTBView model = bll.GetItem(id);
                return View(model);
            }
        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult UpFile()
        {
            CheckTarCode("");
            string definition = string.Empty;
            var file = Request.Files[0];
            if (file.ContentLength > 0)
            {
                HttpPostedFileBase upfile = Request.Files[0];
                System.IO.Stream st = upfile.InputStream;
                System.IO.StreamReader sr = new System.IO.StreamReader(st);
                definition = sr.ReadToEnd();
            }
            Dictionary<string, string> query = Request.Form.ToDictionary();
            IDataParameter[] queryParas = new SqlParameter[query.Count + 1];
            int index = 0;
            string newID = Guid.NewGuid().ToString();
            if (!string.IsNullOrEmpty(query["ID"]))
                newID = query["ID"];
            foreach (string item in query.Keys)
            {
                switch (item)
                {
                    case "ID":
                        queryParas[index] = new SqlParameter("@" + item, newID);
                        break;
                    case "Definition":
                        queryParas[index] = new SqlParameter("@" + item, definition);
                        break;
                    default:
                        queryParas[index] = new SqlParameter("@" + item, query[item]);
                        break;
                }

                index += 1;
            }
            LoginInfo loginInfos = base.loginInfo();
            SysTBViewBLL bll = new SysTBViewBLL(loginInfos);
            queryParas[index] = new SqlParameter("@UserName", loginInfos.Dbuser);
            bll.Update(queryParas, "Update");
            Response.Write("<script>alert('操作成功！');window.location.href='/SysView/ViewList';</script>");
            return View(new Model.SysTBView());
        }
        #endregion
    }
    public class ViewInfo : BaseController
    { 
        /// <summary>
        /// 检查登录系统code
        /// </summary>
        public void CheckTarCode(string dbName)
        {
            HttpCookie myCookie = Request.Cookies["MDM_TargetCode"];
            if (myCookie == null || Request.Cookies["MDM_TargetCode"].Value == "" || Request.Cookies["MDM_dbName"].Value != dbName)// 
            {
                Response.Write("请选择登陆系统！");
                Response.End();
            }
        }
    }
}
